<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>打开控制台查看结果</div>

    <script>

        let state = (a,b) => {

            let obj = {
                sum : a + b,
                remainder : a % b,
                menuList :  a * b
            }
            return obj
            
        }

        /*************/
        let obj = { 
            menu : state => state.menuList,
            menu2 : state => state.sum,
            menu3 : state => state.remainder
        }
        console.log(obj.menu(state(10,5)));
        console.log(obj.menu2(state(10,5)));
        console.log(obj.menu3(state(10,5)));

        console.log('============工具(参数的形式)===========');
 
        /* 工具包，参数不固定，通过rest来判断状态，并确定参数 */
        let util = (...params) => {

            // 最后一个
            if(params.at(-1) == 'at') return params.at(-1)

            // 数组所有数的和
            if(params.at(-1) == 'sum') {
                let sum = 0
                for(let i = 0 ; i < params[0].length; i++ ){
                    sum += params[0][i]
                }
                return sum
            }

            // 数组所有数的和是否是偶数
            if(params.at(-1) == 'sumEven') {
                let sum = 0
                for(let i = 0 ; i < params[0].length; i++ ){
                    sum += params[0][i]
                }
                return sum % 2
            }

            // 数组内是否包含参数
            if(params.at(-1) == 'include') {
                return params[0].includes(params[1])
            }

        }

        console.log(util([1,2,3,4],'sum')); // 10
        console.log(util([1,2,3,4],'at')); // at
        console.log(util([122,233,344,455,366],'sumEven')); // 1不是 0是
        console.log(util([4,2,1,7,6],2,'include')); // true




        console.log('==================对象的形式=================');
                /* 工具包，参数不固定，通过rest来判断状态，并确定参数 */
                let util2 = (...params) => {
                    // 最后一个
                    let at = () =>  params.at(-1)

                    // 数组所有数的和
                    let sum = () => {
                            let sum = 0
                            for(let i = 0 ; i < params[0].length; i++ ){
                                sum += params[0][i]
                            }
                            return sum
                        }

                    // 数组所有数的和是否是偶数
                    let sumEven = () => {
                            let sum = 0
                            for(let i = 0 ; i < params[0].length; i++ ){ sum += params[0][i] }
                            return sum % 2
                    }

                    // 数组内是否包含参数
                    let include = () => params[0].includes(params[1])



                    switch (params.at(-1)) {
                        case 'at':
                             at()
                            break;

                        case 'sum':
                            sum()
                            break;

                        case 'sumEven':
                            sumEven()
                            break;

                        case 'include':
                            include()
                            break;
                    
                        default:
                            break;
                    }

                    let obj = {
                        at : at(),
                        sum : sum(),
                        sumEven : sumEven(),
                        include : include(),
                    }
                    return obj;
                }


                let obj2 = {
                    at : util2 => util2.at,
                    sum : util2 => util2.sum,
                    sumEven : util2 => util2.sumEven,
                    include : util2 => util2.include
                }
                console.log(obj2.at(util2([1,2,3,4,5],'at')));  // at
                console.log(obj2.sum(util2([1,2,3,4,5],'sum')));  // 15
                console.log(obj2.sumEven(util2([1,2,3,4,5],'sumEven'))); // 1
                console.log(obj2.include(util2([1,2,3,4,5],2,'include')));  // true


    </script>
</body>
</html>